package com.njym.java.security.app.rest;


import cn.hutool.core.date.DateUtil;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;

import com.alipay.api.DefaultAlipayClient;

import com.alipay.api.request.AlipayFundTransToaccountTransferRequest;


import com.alipay.api.response.AlipayFundTransToaccountTransferResponse;


import com.google.gson.Gson;
import com.njym.java.security.app.alipay.AliPayProperties;
import com.njym.java.security.app.entity.Alipay;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import sun.nio.cs.ext.GBK;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 * @program: CashOutController
 * @description:
 * @author: wb
 * @create: 2020-02-25 10:07
 **/
@Controller
@RequestMapping("cashOut")
public class CashOutController {
    private static final Logger log = LoggerFactory.getLogger(CashOutController.class);
    @Autowired
    private AliPayProperties aliPayProperties;

    @RequestMapping(value = "/alipays", method = RequestMethod.GET)
    @ResponseBody
    public Object test(){
        String CHARSET = "UTF-8";
        String PAYEE_TYPE = "ALIPAY_LOGONID";//支付宝登录号，支持邮箱和手机号格式。
        //  private String PAYEE_TYPE = "ALIPAY_USERID";//支付宝账号对应的支付宝唯一用户号。以2088开头的16位纯数字组成
        String APP_ID = aliPayProperties.getAppid();
        String ALIPAY_URL = aliPayProperties.getOpenApiDomain();//调用支付地址
        String APP_PRIVATE_KEY = aliPayProperties.getPrivateKey();
        String ALIPAY_PUBLIC_KEY = aliPayProperties.getAlipayPublicKey();
        //实例化接口
        AlipayClient alipayClient = new DefaultAlipayClient(ALIPAY_URL, APP_ID, APP_PRIVATE_KEY, "json", CHARSET, ALIPAY_PUBLIC_KEY, "RSA2");
        AlipayFundTransToaccountTransferRequest request = new AlipayFundTransToaccountTransferRequest();
        Alipay alipay = new Alipay();
        alipay.setOut_biz_no("5566922336585422");
        alipay.setPayee_type(PAYEE_TYPE);
        alipay.setAmount("0.01");
        alipay.setPayer_show_name("测试名称");
        alipay.setPayee_account("15189832998");
        alipay.setPayee_real_name("王镔");
        alipay.setRemark("萌点用户提现");
        //转成json格式放入
        String json = new Gson().toJson(alipay);
        request.setBizContent(json);
        AlipayFundTransToaccountTransferResponse response = null;
        Map<String, Object> map = new HashMap<String,Object>();
        try{
            response = alipayClient.execute(request);
            if("10000".equals(response.getCode())){
                map.put("code", response.getCode());
                map.put("success", "true");
                map.put("des", "转账成功");
            }else{
                map.put("code", response.getCode());
                map.put("sub_code", response.getSubCode());//详情状态码
                map.put("success", "false");
                map.put("sub_msg", response.getSubMsg());//详情原因
            }
        }catch(AlipayApiException e){
            e.printStackTrace();
            map.put("success", "false");
            map.put("des", "转账失败！");
        }
        return map;
    }
}
